﻿package 
{
	import flash.display.*;
	import flash.events.*;
	import flash.utils.*;
	import fl.data.DataProvider;
	import fl.controls.TextInput;
	import fl.controls.CheckBox;
	import flash.text.*;
	import flash.system.Capabilities;
	public class TaskTimer extends MovieClip
	{
		private var this_index:Number;
		private var time:Number;
		private var StartedAt:Number;
		private var StoppedTime:Number;
		private var Active:Boolean;
		private var FileName:String;
		private var TaskNumber:int;
		private var task_name:TextInput;
		private var task_number:TextInput;
		private var select_task:MovieClip;
		private var ActiveThing:MovieClip;
		private var timerSubmit:MovieClip;

		public var start_stop:MovieClip;
		public var intervals_btn:MovieClip;
		private var submitBtn:MovieClip;

		public function TaskTimer(timer_id=null)
		{
			FileName = timer_id;
			this_index = new int();
			time = new int();
			StartedAt = new int();
			StoppedTime = new int();
			TaskNumber = null;
			Active = false;
			task_name = new TextInput();
			task_number = new TextInput();
			
			ActiveThing = active_thing;
			ActiveThing.stop();

			start_stop = new Buttons('startStop');
			start_stop.x = 511;
			start_stop.y = 4;
			this.addChild(start_stop);

			intervals_btn = new Buttons('intervals');
			intervals_btn.x = 441;
			intervals_btn.y = 7;
			this.addChild(intervals_btn);

			submitBtn = new Buttons('submit');
			submitBtn.x = 550;
			submitBtn.y = 5;
			this.addChild(submitBtn);

			var deleteBtn = new Buttons('delete');
			deleteBtn.x = 0;
			deleteBtn.y = 5;
			this.addChild(deleteBtn);

			start_stop.addEventListener(MouseEvent.CLICK, start_stop_me, false, 0, true);
			deleteBtn.addEventListener(MouseEvent.CLICK, delete_me, false, 0, true);
			this.addEventListener(Event.REMOVED, Main.timer_deleted, false, 0, true);
			
			this.timer_count.timer.addEventListener(FocusEvent.FOCUS_OUT, time_changed, false, 0, true);
			this.timer_count.timer.addEventListener(MouseEvent.CLICK, selectAll, false, 0, true);

			submitBtn.addEventListener(MouseEvent.CLICK, submit_me, false, 0, true);
			//submitBtn.setStyle('upSkin', 'submit_upSkin');
			//submitBtn.setStyle('overSkin', 'submit_overSkin');

			this.addChild(task_name);
			task_name.width = 360;
			task_name.y = 3;
			task_name.x = 77;
			task_name.addEventListener(MouseEvent.MIDDLE_CLICK, openTaskSelect, false, 0, true);
			
			intervals_btn.addEventListener(MouseEvent.CLICK, openTaskSelect, false, 0, true);
			intervals_btn.buttonMode = true;
			intervals_btn.stop();

			task_number.setStyle('upSkin', 'blank');
			this.addChild(task_number);
			task_number.width = 50;
			task_number.y = 3;
			task_number.x = 20;
			task_number.addEventListener(FocusEvent.FOCUS_OUT, update_task_number, false, 0, true);
			task_number.addEventListener(MouseEvent.CLICK, testCLICK, false, 0, true);
		}

		// Ticker script
		//task_name.text = task_name.text.substr(1)+task_name.text.substr(0,1); max length 25

		public function selectAll(e)
		{
			actuallySelectAll(e);
		}

		public function actuallySelectAll(e)
		{
			e.target.setSelection(0, e.target.getLineLength(0));
		}

		public function testCLICK(e:MouseEvent)
		{
			task_number.setStyle('upSkin', 'TextInput_upSkin');
			actuallySelectAll(e);
		}
		
		public function submit_me(e:MouseEvent)
		{
			var options:NativeWindowInitOptions = new NativeWindowInitOptions();
			options.systemChrome = NativeWindowSystemChrome.NONE;
			options.type = NativeWindowType.LIGHTWEIGHT;
			options.transparent = true;
			options.maximizable = false;
			options.resizable = false;
			
			//todo = new NativeApplication();
			
			var win:NativeWindow = new NativeWindow(options);
			win.width = 451;
			win.height = 398;
			win.stage.scaleMode = "noScale";
			win.stage.align = StageAlign.TOP_LEFT;
			win.alwaysInFront = true;

			timerSubmit = new TimerSubmit();
			
			win.stage.addChild(timerSubmit);
			win.x = (Capabilities.screenResolutionX - win.width) / 2;
			win.y = (Capabilities.screenResolutionY - win.height) / 2;		
			win.activate();

			if(TaskNumber)
			{
					var intervals = new Intervals(this, 'timer_submit');
					intervals.submit_timer(TaskNumber);
				try
				{
					//Intervals.submit_timer('<?xml version="1.0" encoding="UTF-8"?>'+xml);
				}
				catch(error:IOErrorEvent) {
					new Errors("IOErrorEvent catch: " + error);
					
				} catch(error:TypeError) {
					new Errors("TypeError catch: " + error);
					
				} catch(error:Error) {
					new Errors("Error catch: " + error);
					
				}
			}
		}
		
		function update_task_number(e:FocusEvent):void
		{
			task_number.setStyle('upSkin', 'blank');
			if(e.target.text.match(/[\D]/))
			{
				e.target.text='';
				TaskNumber=null;
				return;
			}
			var pattern:RegExp = /[\D]/g;
			if(e.target.text!=TaskNumber && e.target.text.replace(pattern,'')!='')
			{
				var intervals:Intervals = new Intervals(this, 'get_task');
				intervals.get_task(e.target.text);
			}
		}
		
		function time_changed(e:FocusEvent):void
		{
			update_time(e.target.text);
		}
		
		public function update_time(new_time:String)
		{
			if(new_time.indexOf(':')!=-1)
			{
				time = Tools.unformatTime(new_time);
			}
			else if(new_time.indexOf('.')!=-1)
			{
				time = new_time*1000*60*60;
			}
			else
			{
				time = new_time;
			}
			this.timer_count.timer.text = Tools.formatTime(time);
		}
		
		function openTaskSelect(e:MouseEvent):void
		{
			Main.set_index_of_selected(this_index);
			var options:NativeWindowInitOptions = new NativeWindowInitOptions();
			options.systemChrome = NativeWindowSystemChrome.NONE;
			options.type = NativeWindowType.LIGHTWEIGHT;
			options.transparent = true;
			options.maximizable = false;
			options.resizable = false;
			
			var win:NativeWindow = new NativeWindow(options);
			win.width = 601;
			win.height = 397;
			win.x = 300;
			win.y = 100;
			win.stage.scaleMode = "noScale";
			win.stage.align = StageAlign.TOP_LEFT;
			win.alwaysInFront = true;
			
			select_task = new TaskSelect();
			
			win.stage.addChild(select_task);
			win.activate();
			
			win.x = (Capabilities.screenResolutionX - win.width) / 2;
			win.y = (Capabilities.screenResolutionY - win.height) / 2;		
		}

		public function set_task(task:Object, logit=true)
		{
			try
			{
				if(task.title)
				{
					task_name.text = task.title;
				}

				set_task_number(task.localid, logit);
				if(logit)
				{
					var new_task:Tasks = new Tasks();
					new_task.set_details(task);
					new_task.insert();
					delete(new_task);
				}
				task_number.setStyle('upSkin', 'blank');
			}
			catch(e){ new Errors('Some sort if task setting issue...\n'+e); }
		}

		public function get_task_failed()
		{
			task_number.text = '';
			task_number.setStyle('upSkin', 'blank');
		}

		private function start_stop_me(e:Event)
		{
			if (Main.ActiveTimer != this_index)
			{
				StartedAt = getTimer() - time;
				if(Main.ActiveTimer)
				{
					Main.Timers[Main.ActiveTimer].start_stop.action('pause');
					Main.Timers[Main.ActiveTimer].ActiveThing.gotoAndStop(1);
				}
				Main.ActiveTimer = this_index;
				start_stop.action('play');
				ActiveThing.gotoAndStop(2);
			}
			else
			{
				Main.ActiveTimer = undefined;
				start_stop.action('pause');;
				ActiveThing.gotoAndStop(1);
			}
		}

		private function delete_me(e:Event)
		{
			delete_timer();
		}

		public function delete_timer()
		{
			log_me(true);
			Main.index_of_removed = this_index;

			if (Main.ActiveTimer == this_index)
			{
				Main.ActiveTimer = undefined;
			}
			delete Main.Timers[this_index];
			//Main.timer_deleted();
			parent.removeChild(this);
		}

		public function get_start_time()
		{
			return StartedAt;
		}

		public function get_stopped_time()
		{
			var this_stopped_time = StoppedTime;
			StoppedTime = 0;
			return this_stopped_time;
		}

		public function set_index(index:Number)
		{
			this_index = index;
		}

		public function get_index()
		{
			return this_index;
		}

		public function set_task_number(number:String, logit=true)
		{
			//Logger.remove_old_log(FileName);
			try
			{
				task_number.text = number;
			}
			catch(e){}
			if(number.match(/[\D]/))
			{
				TaskNumber=null;
			}
			else
			{
				TaskNumber = number;
			}
			if(logit)
			{
				log_me();
			}
		}

		public function get_task_number()
		{
			return TaskNumber;
		}
		
		public function get_timer_description()
		{
			return task_name.text;
		}
		
		public function set_time(i)
		{
			time = i;
			this.timer_count.timer.text=Tools.formatTime(i);
		}
		
		public function get_time()
		{
			return time;
		}

		public function get_as_object()
		{
			var timer:Object = {
				task_number : TaskNumber,
				description : task_name.text
				time : time
			};
			return timer;
		}
		
		public function log_me(delete_me=false)
		{
			if(FileName)
			{
				var timerLog:TimerLog = new TimerLog();
				
				timerLog.timer_id = FileName;
				timerLog.task_number = TaskNumber;
				timerLog.description = task_name.text;
				timerLog.time = time;
				timerLog.date = new Date();
				timerLog.deleted = delete_me;
				timerLog.update();
			}
		}
	}
}